package com.brainshare.web.controller.an;

import com.brainshare.common.annotation.Anonymous;
import com.brainshare.common.core.controller.BaseController;
import com.brainshare.common.core.domain.AjaxResult;
import com.brainshare.common.core.page.TableDataInfo;
import com.brainshare.common.utils.DateUtils;
import com.brainshare.common.utils.poi.ExcelUtil;
import com.brainshare.system.domain.SysBuriedPoint;
import com.brainshare.system.domain.param.BuriedPointParam;
import com.brainshare.system.domain.query.BuriedPointQuery;
import com.brainshare.system.domain.vo.*;
import com.brainshare.system.service.IRedPacketRecordService;
import com.brainshare.system.service.ISysBuriedPointResultService;
import com.brainshare.system.service.ISysBuriedPointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 埋点接口
 * @author wll
 */
@RestController
@RequestMapping("/share/buried_point/v1")
public class BuriedPointController extends BaseController {

    @Autowired
    private ISysBuriedPointService sysBuriedPointService;

    @Autowired
    private ISysBuriedPointResultService sysBuriedPointResultService;

    @Autowired
    private IRedPacketRecordService redPacketRecordService;

    @Anonymous
    @PostMapping("/add")
    public AjaxResult addBuriedPoint(HttpServletRequest request, @RequestBody BuriedPointParam buriedPointParam) {
        Integer result = sysBuriedPointService.addBuriedPoint(request, buriedPointParam);
        if (null==result){
            return error();
        }
        return success(result);
    }

    /** 注册 **/

    @Anonymous
    @PostMapping("/get_login_sum")
    public AjaxResult getLoginSum(@RequestBody BuriedPointQuery buriedPointQuery) {
        return success(sysBuriedPointResultService.getLoginSum(buriedPointQuery));
    }

    @Anonymous
    @PostMapping("/get_login_detail")
    public TableDataInfo getLoginDetail(@RequestBody BuriedPointQuery buriedPointQuery) {
        startPage();
        List<LoginDetailVO> loginDetailList = sysBuriedPointResultService.getLoginDetailList(buriedPointQuery);
        return getDataTable(loginDetailList);
    }

    @Anonymous
    @PostMapping("/export_login_detail")
    public void exportLoginDetail(HttpServletResponse response, BuriedPointQuery buriedPointQuery) {
        List<LoginDetailVO> loginDetailList = sysBuriedPointResultService.getLoginDetailList(buriedPointQuery);
        ExcelUtil<LoginDetailVO> util = new ExcelUtil<LoginDetailVO>(LoginDetailVO.class);
        String dateTime= DateUtils.dateTimeNow();
        util.exportExcel(response, loginDetailList, "注册明细"+dateTime);
    }

    /** 筛查 **/

    @Anonymous
    @PostMapping("/get_an_sum")
    public AjaxResult getAnCompleteSum(@RequestBody BuriedPointQuery buriedPointQuery) {
        Long completeSum = sysBuriedPointResultService.getAnCompleteSum(buriedPointQuery);
        return AjaxResult.success("",completeSum);
    }

    @Anonymous
    @PostMapping("/get_an_detail")
    public TableDataInfo getAnDetail(@RequestBody BuriedPointQuery buriedPointQuery) {
        startPage();
        List<StatisticsAnDetailVO> anDetailList = sysBuriedPointResultService.getAnDetailList(buriedPointQuery);
        if (null==anDetailList){
            return getDataTable();
        }
        return getDataTable(anDetailList);
    }

    @Anonymous
    @PostMapping("/export_an_detail")
    public void exportAnDetail(HttpServletResponse response, BuriedPointQuery buriedPointQuery) {
        List<StatisticsAnDetailVO> anDetailList = sysBuriedPointResultService.getAnDetailList(buriedPointQuery);
        ExcelUtil<StatisticsAnDetailVO> util = new ExcelUtil<StatisticsAnDetailVO>(StatisticsAnDetailVO.class);
        String dateTime= DateUtils.dateTimeNow();
        util.exportExcel(response, anDetailList, "筛查明细"+dateTime);
    }

    /** 提取金额 **/

    @Anonymous
    @PostMapping("/get_withdrawal_money_sum")
    public AjaxResult getWithdrawalMoneySum(@RequestBody BuriedPointQuery buriedPointQuery) {
        String withdrawalMoneySum = redPacketRecordService.getWithdrawalMoneySum(buriedPointQuery);
        return AjaxResult.success("",withdrawalMoneySum);
    }

    @Anonymous
    @PostMapping("/get_withdrawal_money_detail")
    public TableDataInfo getWithdrawalMoneyDetailList(@RequestBody BuriedPointQuery buriedPointQuery) {
        startPage();
        List<StatisticsWithdrawalMoneyDetailVO> withdrawalMoneyDetailList1 = redPacketRecordService.getWithdrawalMoneyDetailList(buriedPointQuery);
        List<StatisticsWithdrawalMoneyDetailVO> withdrawalMoneyDetailList2 = redPacketRecordService.getWithdrawalMoneyDetailList(withdrawalMoneyDetailList1);
        if (null==withdrawalMoneyDetailList2){
            return getDataTable();
        }
        return getDataTableNew(withdrawalMoneyDetailList1,withdrawalMoneyDetailList2);
    }

    @Anonymous
    @PostMapping("/export_withdrawal_money_detail")
    public void exportWithdrawalMoneyDetailList(HttpServletResponse response,BuriedPointQuery buriedPointQuery) {
        List<StatisticsWithdrawalMoneyDetailVO> withdrawalMoneyDetailList = redPacketRecordService.getWithdrawalMoneyDetailList(buriedPointQuery);
        List<StatisticsWithdrawalMoneyDetailVO> detailList = redPacketRecordService.getWithdrawalMoneyDetailList(withdrawalMoneyDetailList);
        ExcelUtil<StatisticsWithdrawalMoneyDetailVO> util = new ExcelUtil<StatisticsWithdrawalMoneyDetailVO>(StatisticsWithdrawalMoneyDetailVO.class);
        String dateTime= DateUtils.dateTimeNow();
        util.exportExcel(response, detailList, "提取金额明细"+dateTime);
    }

    /** 训练营转化 **/
    @Anonymous
    @PostMapping("/get_train_camp_transfer_sum")
    public AjaxResult getTrainCampTransferSum(@RequestBody BuriedPointQuery buriedPointQuery) {
        Long shareSum = redPacketRecordService.getTrainCampTransferSum(buriedPointQuery);
        return AjaxResult.success("",shareSum);
    }

    @Anonymous
    @PostMapping("/get_train_camp_transfer_detail")
    public TableDataInfo getTrainCampTransferDetailList(@RequestBody BuriedPointQuery buriedPointQuery) {
        startPage();
        List<StatisticsTrainCampTransformDetailVO> trainCampTransferDetailList1 = redPacketRecordService.getTrainCampTransferDetailList(buriedPointQuery);
        List<StatisticsTrainCampTransformDetailVO> trainCampTransferDetailList2 = redPacketRecordService.getTrainCampTransferDetailList(trainCampTransferDetailList1);
        if (null == trainCampTransferDetailList2) {
            return getDataTable();
        }
        return getDataTableNew(trainCampTransferDetailList1, trainCampTransferDetailList2);
    }

    @Anonymous
    @PostMapping("/export_train_camp_transfer_detail")
    public void exportTrainCampTransferDetailList(HttpServletResponse response,BuriedPointQuery buriedPointQuery) {
        List<StatisticsTrainCampTransformDetailVO> trainCampTransferDetailList = redPacketRecordService.getTrainCampTransferDetailList(buriedPointQuery);
        ExcelUtil<StatisticsTrainCampTransformDetailVO> util = new ExcelUtil<StatisticsTrainCampTransformDetailVO>(StatisticsTrainCampTransformDetailVO.class);
        String dateTime= DateUtils.dateTimeNow();
        util.exportExcel(response, trainCampTransferDetailList, "训练营转化明细"+dateTime);
    }

    /** 分享 **/
    @Anonymous
    @PostMapping("/get_share_sum")
    public AjaxResult getShareSum(@RequestBody BuriedPointQuery buriedPointQuery) {
        Long shareSum = sysBuriedPointResultService.getShareSum(buriedPointQuery);
        return AjaxResult.success("",shareSum);
    }

    @Anonymous
    @PostMapping("/get_share_detail")
    public TableDataInfo getShareDetailList(@RequestBody BuriedPointQuery buriedPointQuery) {
        startPage();
        List<StatisticsShareDetailVO> shareDetailList1 = sysBuriedPointResultService.getShareDetailList(buriedPointQuery);
        List<StatisticsShareDetailVO> shareDetailList2 = sysBuriedPointResultService.getShareDetailList(shareDetailList1);
        if (null == shareDetailList2){
            return getDataTable();
        }
        return getDataTableNew(shareDetailList1, shareDetailList2);
    }

    @Anonymous
    @PostMapping("/export_share_detail")
    public void exportShareDetailList(HttpServletResponse response, BuriedPointQuery buriedPointQuery) {
        List<StatisticsShareDetailVO> shareDetailList1 = sysBuriedPointResultService.getShareDetailList(buriedPointQuery);
        List<StatisticsShareDetailVO> shareDetailList2 = sysBuriedPointResultService.getShareDetailList(shareDetailList1);
        ExcelUtil<StatisticsShareDetailVO> util = new ExcelUtil<StatisticsShareDetailVO>(StatisticsShareDetailVO.class);
        String dateTime = DateUtils.dateTimeNow();
        util.exportExcel(response, shareDetailList2, "分享明细" + dateTime);
    }

    @Anonymous
    @PostMapping("/get_buried_point_detail")
    public TableDataInfo getBuriedPointDetail( @RequestBody BuriedPointQuery buriedPointQuery) {
        startPage();
        List<SysBuriedPoint> sysBuriedPointList = sysBuriedPointService.getBuriedPointList(buriedPointQuery);
        List<SysBuriedPointVO> sysBuriedPointVOList = sysBuriedPointService.getSysBuriedPointVO(sysBuriedPointList);
        if (null==sysBuriedPointVOList){
            return getDataTable();
        }
        return getDataTableNew(sysBuriedPointVOList, sysBuriedPointList);
    }

    @Anonymous
    @PostMapping("/export_buried_point_detail")
    public void exportBuriedPointDetail(HttpServletResponse response, BuriedPointQuery buriedPointQuery) {
        List<SysBuriedPoint> sysBuriedPointList = sysBuriedPointService.getBuriedPointList(buriedPointQuery);
        List<SysBuriedPointVO> sysBuriedPointVOList = sysBuriedPointService.getSysBuriedPointVO(sysBuriedPointList);
        ExcelUtil<SysBuriedPointVO> util = new ExcelUtil<SysBuriedPointVO>(SysBuriedPointVO.class);
        String dateTime= DateUtils.dateTimeNow();
        util.exportExcel(response, sysBuriedPointVOList, "埋点明细"+dateTime);
    }

    @Anonymous
    @PostMapping("/add_buried_point_result")
    public void addBuriedPointResult(String date) {
        sysBuriedPointResultService.addBuriedPointResult(date);
    }
}
